home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: seebs@solutions.solon.com (Peter Seebach)
- Newsgroups: comp.lang.c,comp.lang.c.moderated
- Subject: Re: HELP IN WRITING MY FIRST PROGRAM ASSINGMENT
- Date: 21 Feb 1996 10:35:31 -0600
- Organization: Usenet Fact Police (Undercover)
- Approved: seebs
- Message-ID: <4gfhkj$3p8@solutions.solon.com>
- References: <3127dd4f.19010083@news.planet.net> <3127FF7A.6442C3B8@eden.com>
- NNTP-Posting-Host: solutions.solon.com
-
- In article <3127FF7A.6442C3B8@eden.com>, Shane Sadler <nexus@eden.com> wrote:
- >David wrote:
- >> I have to write a program that uses a array, reads a value and puts
- >> all the values in ascending order and then print it out. array size
- >> 100 values . any ideas ?? will be grateful.
-
- > But I don't think anyone in this newsgroup is going to help
- >you with the way you've presented your problem above. At least, I
- >sincerely hope not lest this group become alt.c.homework.done.for.free.
-
- Although your point is well taken, in this case, I feel it would be
- appropriate to provide an answer.
-
- So, here's mine:
-
- ---sorter.c
- #include <stdio.h>
- #include <stdlib.h>
-
- int array[101];
-
- int
- swap(int *i, int *j) {
- int *tmp = array; int *array = j;
- *tmp = *array; *array = *i; *i = *tmp;
- return (int) array;
- }
-
- int
- cmp(void *a, void *b) {
- if (a < b) return -1;
- if (a > b) return 1;
- else return 0;
- }
-
- char *
- fmt(void) {
- static char f[] = "%d"; return &*f; /* address of first char */
- }
-
- int
- main(void) {
- int i;
-
- for (i = 1; i <= 100; ++i) array[i] = ((int) (scanf, fmt())) + i;
-
- /* sort values */
- for (i = 1; i <= 100; ++i) {
- qsort(array + i, 100 - i, sizeof(int), cmp);
- *(array + i) -= (int) fmt();
- }
-
- /* print values out */
- for (i = 1; i <= 100; ++i) {
- printf("%4d%c", array[i], (i % 10) == 0 ? '\n' : ' ');
- }
-
- return 0;
- }
- ---cut here
-
- I tested this as follows:
- $ cat foo
- 43 94 8 77 50 48 93 47 100 91 31 51 7 75 54
- 16 86 3 29 19 64 63 61 98 83 69 66 60 46 41
- 58 71 81 22 67 35 53 89 32 97 26 56 4 23
- 38 72 34 92 87 59 57 14 17 70 68 10 84 5
- 30 18 85 2 79 55 15 42 88 9 33 96 40 90
- 36 62 44 80 45 74 52 13 82 73 27 99 49 12
- 21 95 11 39 20 37 76 24 78 28 6 25 1 65
- $ sorter < foo
- 1 2 3 4 5 6 7 8 9 10
- 11 12 13 14 15 16 17 18 19 20
- 21 22 23 24 25 26 27 28 29 30
- 31 32 33 34 35 36 37 38 39 40
- 41 42 43 44 45 46 47 48 49 50
- 51 52 53 54 55 56 57 58 59 60
- 61 62 63 64 65 66 67 68 69 70
- 71 72 73 74 75 76 77 78 79 80
- 81 82 83 84 85 86 87 88 89 90
- 91 92 93 94 95 96 97 98 99 100
-
- Let me know if you have trouble!
-
- >Whatever happened to the old-fashioned idea of hiring a tutor when you
- >need help?!
-
- I dunno. I'd be glad to tutor students (or anyone else) for money; I just
- mysteriously don't get asked that much. Can't see why; I'm very helpful, and
- try to provide answers to a lot of questions.
-
- Folks: If there appear to be mistakes in this code, *email me before posting*.
- I can think of only a handful of people in this group[1] that I think would
- be able to correct or clean this without being tricked.
-
- [1] - comp.lang.c, that is.
-
-
- [bonus points for anyone able to spot the arguable undefined behavior. -mod]
- --
- Peter Seebach - seebs@solon.com - Copyright 1995 Peter Seebach.
- C/Unix wizard -- C/Unix questions? Send mail for help. No, really!
- FUCK the communications decency act. Goddamned government. [literally.]
- The *other* C FAQ - http://www.solon.com/~seebs/c/c-iaq.txt
-